package comparisons

import (
	"JVM/Jvmgo/GoToJVM/instructions/base"
	"JVM/Jvmgo/GoToJVM/runtimeData"
)

/*
比较指令，这里比较 栈顶两个double类型
*/
type DCMPG struct{ base.NoOperandsInstruction }

func (self *DCMPG) Execute(frame *runtimeData.Frame) {
	_dcmp(frame, true)
}

type DCMPL struct{ base.NoOperandsInstruction }

func (self *DCMPL) Execute(frame *runtimeData.Frame) {
	_dcmp(frame, false)
}

func _dcmp(frame *runtimeData.Frame, gFlag bool) {
	stack := frame.OperandStack()
	val2 := stack.PopDouble()
	val1 := stack.PopDouble()
	if val1 > val2 {
		stack.PushInt(1)
	} else if val1 == val2 {
		stack.PushInt(0)
	} else if val1 < val2 {
		stack.PushInt(-1)
	} else if gFlag {
		stack.PushInt(1)
	} else {
		stack.PushInt(-1)
	}
}
